package com.xcdh.trading.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcdh.trading.entity.HotMoneyTrc;
import com.xcdh.trading.entity.StockInfo;
import com.xcdh.trading.mapper.HotMoneyTrcMapper;
import com.xcdh.trading.response.TradingRes;
import com.xcdh.trading.service.HotMoneyTrcService;
import com.xcdh.trading.service.StockInfoService;
import com.xcdh.trading.util.TuShareApi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HotMoneyTrcServiceImpl extends ServiceImpl<HotMoneyTrcMapper, HotMoneyTrc>
    implements HotMoneyTrcService {
    @Autowired
    private HotMoneyTrcMapper hotMoneyTrcMapper ;

    @Autowired
    private StockInfoService stockInfoService ;
    @Override
    public void syncHotMoneyTrc(String dateCode) {
        TradingRes tradingRes = TuShareApi.post("hm_detail", dateCode);
        List<HotMoneyTrc> moneyTrcList = tradingRes.parse(HotMoneyTrc.class);



        for (HotMoneyTrc hotMoneyTrc : moneyTrcList) {
            HotMoneyTrc moneyTrc = lambdaQuery()
                    .eq(HotMoneyTrc::getTsCode, hotMoneyTrc.getTsCode())
                    .eq(HotMoneyTrc::getHmName ,hotMoneyTrc.getHmName() )
                    .eq(HotMoneyTrc::getTradeDate, dateCode).one();
            if(moneyTrc == null){
                save(hotMoneyTrc) ;
            }
        }
    }

    @Override
    public void updateHotMoneyTrc(String dateCode) {
        List<HotMoneyTrc> moneyTrcs = lambdaQuery()
                .eq(HotMoneyTrc::getTradeDate, dateCode).list();
        List<String> tsCodes = moneyTrcs.stream()
                .map(HotMoneyTrc::getTsCode)
                .collect(Collectors.toList());
        Map<String, StockInfo> infoMap = stockInfoService.lambdaQuery().in(StockInfo::getTsCode, tsCodes)
                .eq(StockInfo::getTradeDate, dateCode)
                .list().stream().collect(Collectors.toMap(StockInfo::getTsCode, item -> item));
        for (HotMoneyTrc moneyTrc : moneyTrcs) {
            String tsCode = moneyTrc.getTsCode();
            StockInfo info = infoMap.get(tsCode);
            if(info != null){
                moneyTrc.setPrice(info.getPrice().floatValue());
                moneyTrc.setPctChg(info.getPctChg().floatValue());
                moneyTrc.setTurnoverRate(info.getTurnoverRate());
                moneyTrc.setVolumeRatio(info.getVolumeRatio());
                moneyTrc.setNums(info.getNums());
                updateById(moneyTrc) ;
            }
        }
    }

    @Override
    public List<HotMoneyTrc> statisticsBuyMoney(List<String> tsCodes) {
        if(tsCodes.isEmpty()){
            return new ArrayList<>() ;
        }
        return hotMoneyTrcMapper.statisticsBuyMoney(tsCodes , DateUtil.format(new Date() , "yyyyMMdd"));
    }

    @Override
    public List<HotMoneyTrc> statisticsBuyMoney(List<String> tsCodes, String tradeDate) {
        if(tsCodes.isEmpty()){
            return new ArrayList<>() ;
        }
        if(StringUtils.isBlank(tradeDate)){
            return statisticsBuyMoney(tsCodes) ;
        }
        return hotMoneyTrcMapper.statisticsBuyMoney(tsCodes ,tradeDate );
    }

    @Override
    public List<HotMoneyTrc> statisticsBuyMoneyByTradeDate(String tradeDate) {
        return hotMoneyTrcMapper.statisticsBuyMoneyByTradeDate(tradeDate);
    }
}
